﻿package com.kennycosca.util {
	
	//
	// http://code.google.com/p/as3corelib/
	// from adobe as3 corelib
	//
	import com.adobe.serialization.json.JSON;	

	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;

	/**
	 * @author kentendo
	 */

	
	public class TextFileLoader extends EventDispatcher {
		
		public static const LOADED:String = "loaded";
		public static const FAILED:String = "failed";
		
		public static const TEXT_MODE:String = "text_mode";
		public static const BINARY_MODE:String = "binary_mode";
		public static const VARIABLES_MODE:String = "variables_mode";
		public static const JSON_MODE:String = "json_mode";
        
        public var _rawData:*;
        public var _data:*;
        
        private var _txtLoader:URLLoader;
        private var _loaded:Boolean = false;
        private var _txtUrl:String;
        private var _mode:String;

		public function TextFileLoader(txtUrl:String = null, mode:String = TEXT_MODE) {
			trace("TextFileLoader Constructor");
			_txtUrl = txtUrl;
			_mode = mode;
			if (_txtUrl == null) failed();
			if (_txtUrl != null) load();
		}
		
		public function get isLoaded():Boolean {
			return _loaded;
		}
		
		public function get rawData():* {
			return _rawData;
		}
		
		public function get url():String {
			return _txtUrl;
		}
		
		public function get data():* {
			switch(_mode) {
				case BINARY_MODE:
					_data = _rawData as ByteArray;
					break;
				case VARIABLES_MODE:
					_data = new URLVariables(_rawData) as Object;
					break;
				case TEXT_MODE:
					_data = _rawData as String;
					break;
				case JSON_MODE:
					_data = JSON.decode(_rawData) as Object;
					break;
				default :
					_data = _rawData as String;
			}
			
			return _data;	
		}
		
		public function load():void {
			trace("TextFileLoader.load");
			switch(_mode) {
				case BINARY_MODE: 
					trace("TextFileLoader.load.BINARY_MODE");
					loadBinary();
					break;
				case VARIABLES_MODE: 
					trace("TextFileLoader.load.VARIABLES_MODE");
					loadVariables();
					break;
				case TEXT_MODE:
					trace("TextFileLoader.load.TEXT_MODE");
					loadText();
					break;
				case JSON_MODE:
					trace("TextFileLoader.load.JSON_MODE");
					loadText();
					break;
				default :
					trace("TextFileLoader.load.default");
					loadText();
			}
		}
		
		private function loadText():void {
			trace("TextFileLoader.loadText._txtUrl: " + _txtUrl);
			_txtLoader = new URLLoader();
			_txtLoader.dataFormat = URLLoaderDataFormat.TEXT;
			initListeners(_txtLoader);
			_txtLoader.load(new URLRequest(_txtUrl));
		}
		
		private function loadVariables():void {
			trace("TextFileLoader.loadVariables._txtUrl: " + _txtUrl);
			_txtLoader = new URLLoader();
			_txtLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
			initListeners(_txtLoader);
			_txtLoader.load(new URLRequest(_txtUrl));
		}
		
		private function loadBinary():void {
			trace("TextFileLoader.loadBinary._txtUrl: " + _txtUrl);
			_txtLoader = new URLLoader();
			_txtLoader.dataFormat = URLLoaderDataFormat.BINARY;
			initListeners(_txtLoader);
			_txtLoader.load(new URLRequest(_txtUrl));
		}

		protected function txtCompleteHandler(event:Event):void {
		    trace("TextFileLoader.txtCompleteHandler: " + _txtUrl);
		    _rawData = event.target["data"];
		    loaded();
		}
		
		public function loaded():void {
			if (_loaded) return;
			trace("TextFileLoader.loaded event firing");
			dispatchEvent(new Event(LOADED));
			_loaded = true;
		}
		
		private function failed():void {
			dispatchEvent(new Event(FAILED));
		}
		
		private function txtIOErrorHandler(event:IOErrorEvent):void {
		    trace("TextFileLoader.configIOErrorHandler");
		    failed();
		}
		
		private function txtSecurityErrorHandler(event:SecurityErrorEvent):void {
		    trace("TextFileLoader.configSecurityErrorHandler");
		    failed();
		}
		
		private function initListeners(dispatcher:EventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, txtCompleteHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, txtIOErrorHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, txtSecurityErrorHandler);
		}
	}
}